<?php

// $Id$


require_once($WSC->get_site_inc_dir().'modules/tangra_cms/_sys/tcms_admin_web_page_wed_ua.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpage_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/category/tcms_category_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpage_category_dbc.class.php');


class vpage_Categories_WP extends TCMS_Admin_Web_Page_WED_UA {
	public function create_view($name = 'default') {
		$ret = new TCMS_Admin_Page_View($this, $name, 'modules/tangra_cms/pages/_vpage/');

		return $ret;
	}


	protected function init_transit_vars() {
		$this->add_transit_var('vpage');
		$this->add_transit_var('category');
	}


	public function init() {
		parent::init();

		$v = $this->create_view();
		$this->add_view($v);


		$add_event = new Web_Event_Simple_Int('add_event', Tangra_Parameter_Method::GET, 'add');
		$remove_event = new Web_Event_Simple_Int('remove_event', Tangra_Parameter_Method::GET, 'remove');

		$this->wed->add_wed_event_action_pair($add_event, 'add_event');
		$this->wed->add_wed_event_action_pair($remove_event, 'remove_event');
	}


	private function export_vpage_details(DB_Connection $dbc, $vpage_id) {
		$vpage = new TCMS_vpage_DBC();
		if ($vpage->load_by_id($dbc, $vpage_id)) {
			$this->export('vpage', $vpage);
		} else {
			throw new Tangra_Module_Exception('Cannot find vpage item with id = '.$vpage_id);
		}
	}


	private function prepare_category_array(DB_Connection $dbc, TCMS_Category_DBC $category, $vpage_id) {
		$ret = array();

		$ret['title'] = $category->get_title();
		$ret['id'] = $category->get_id();
		$cc_id = TCMS_vpage_Category_DBC::vpage_category_exists($dbc, $vpage_id, $category->get_id());
		$ret['added'] = $cc_id;


		$subcategories = $category->get_subcategories();
		foreach($subcategories as $cat) {
			$ret['subcategories'][] = $this->prepare_category_array($dbc, $cat, $vpage_id);
		}

		return $ret;
	}


	public function add_event($category_id) {
		$vpage = new TCMS_VPage_DBC();
		$dbc = $this->get_dbc();

		if ($vpage->load_by_id($dbc,  $this->get_transit_var('vpage'))) {
			$category = new TCMS_Category_DBC();
			if ($category->load_by_id($dbc, $category_id)) {
				if (!TCMS_VPage_Category_DBC::vpage_category_exists($dbc, $this->get_transit_var('vpage'), $category_id)) {
					$ca = new TCMS_VPage_Category_DBC();
					$ca->set_vpage($this->get_transit_var('vpage'));
					$ca->set_category($category_id);
					$ca->set_ordering(TCMS_VPage_Category_DBC::get_next_ordering($dbc, $category_id));

					try {
						$dbc->start_trans();
						$ca->save($dbc);
						$dbc->complete_trans();
					} catch(Exception $e) {
						$dbc->fail_trans();
						$dbc->complete_trans();
						throw $e;
					}
				}
			} else {
				throw new Tangra_Module_Exception('Cannot find category with id = '.$category_id);
			}
		} else {
			throw new Tangra_Module_Exception('Cannot find vpage item with id = '.$this->get_transit_var('vpage'));
		}

		return $this->default_event();
	}


	public function remove_event($id) {
		$vpage = new TCMS_VPage_DBC();
		$dbc = $this->get_dbc();

		try {
			$dbc->start_trans();
			TCMS_VPage_Category_DBC::delete($dbc, $id);
			$dbc->complete_trans();
		} catch(Exception $e) {
			$dbc->fail_trans();
			$dbc->complete_trans();
			throw $e;
		}

		return $this->default_event();
	}


	protected function default_event() {
		$dbc = $this->get_dbc();

		$vpage_id = $this->get_transit_var('vpage');
		if (!$vpage_id) {
			throw new Tangra_Module_Exception('Expecting "_transit_vpage" parameter');
		}

		$this->export_vpage_details($dbc, $vpage_id);


		$categories = TCMS_Category_DBC::load_categories_by_parent($dbc, 0);
		foreach($categories as $category) {
			$categories_exp[] = $this->prepare_category_array($dbc, $category, $vpage_id);
		}

		$this->export('categories', $categories_exp);

		$view = $this->get_view('default');

		return $view;
	}
}

